package com.mxpio.erp.plm.controller;

import com.mxpio.erp.common.plm.entity.ProjectInfoChange;
import com.mxpio.erp.common.plm.entity.ProjectPauseOperation;
import com.mxpio.erp.common.plm.service.ProjectPauseOperationService;
import com.mxpioframework.common.vo.Result;
import com.mxpioframework.jpa.query.Criteria;
import com.mxpioframework.jpa.query.Operator;
import com.mxpioframework.security.util.SecurityUtils;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.io.UnsupportedEncodingException;
import java.util.List;
import java.util.Map;

@Tag(name = "ProjectPauseOperationController", description = "项目暂停操作历史")
@RestController
@RequestMapping("/erp/plm/pro/pause/operation/")
public class ProjectPauseyOperationController {

    @Autowired
    private ProjectPauseOperationService projectPauseOperationService;

    @GetMapping("list")
    @Operation(summary = "暂停申请列表", description = "暂停申请列表（无分页）", method = "GET")
    public Result<List<ProjectPauseOperation>> list(@Parameter(description="查询参数") Criteria criteria) throws UnsupportedEncodingException {
        List<ProjectPauseOperation> list = projectPauseOperationService.list(ProjectPauseOperation.class, criteria);
        return Result.OK(list);
    }

    @GetMapping("page")
    @Operation(summary = "暂停申请列表", description = "暂停申请列表（分页）", method = "GET")
    public Result<Page<ProjectPauseOperation>> page(@Parameter(description="查询参数") Criteria criteria, @Parameter(description="每页条数")Integer pageSize, @Parameter(description="页号")Integer pageNo) throws UnsupportedEncodingException {
        Pageable page = PageRequest.of(pageNo-1, pageSize);
        Page<ProjectPauseOperation> pageResult = projectPauseOperationService.listPage(ProjectPauseOperation.class, page, criteria);
        return Result.OK(pageResult);
    }

    @GetMapping("list/{bizNo}")
    @Operation(summary = "根据申请单号-获取申请信息", description = "根据申请单号-获取申请信息", method = "GET")
    public Result<ProjectPauseOperation> getById(@PathVariable(name = "bizNo", required = true) String bizNo) {
        ProjectPauseOperation pauseOperation = projectPauseOperationService.getById(ProjectPauseOperation.class, bizNo);
        return Result.OK(pauseOperation);
    }

//    @RequestMapping("add")
    @PostMapping("add/{key}")
    @Operation(summary = "保存暂停信息", description = "保存暂停信息",method = "POST")
    public Result<?> add(
                         @PathVariable(name = "key") String key,
                         @RequestParam(value="businessKey", required = false) String businessKey,
                         @RequestBody Map<String, Object> properties) {
//        @RequestBody ProjectPauseOperation pauseOperation,
//        return projectPauseOperationService.saveAll(pauseOperation);
        return projectPauseOperationService.saveAll(key, SecurityUtils.getLoginUsername(), businessKey, properties);
    }


    @DeleteMapping(value = "/delete/{bizNo}")
    @Operation(summary = "删除暂停信息",description = "删除暂停信息", method = "DELETE")
    public Result<?> delete(@PathVariable(name = "bizNo") String bizNo) {
        projectPauseOperationService.delete(bizNo, ProjectPauseOperation.class);
        return Result.OK("删除成功");
    }

    @PutMapping("edit")
    @Operation(summary = "编辑信息", description = "编辑信息", method = "PUT")
    public Result<ProjectPauseOperation> edit(@RequestBody ProjectPauseOperation pauseOperation) {
        projectPauseOperationService.update(pauseOperation);
        return Result.OK(pauseOperation);
    }

    /**
     * 暂停项目
     * @param pauseOperation
     * @return
     */
    @PutMapping("stop")
    @Operation(summary = "暂停项目",description = "暂停项目",method = "PUT")
    public Result<?> stop(@RequestBody ProjectPauseOperation pauseOperation) {
        return projectPauseOperationService.stopProById(pauseOperation);
    }

    @GetMapping("list/all/{proCode}")
    @Operation(summary = "根据项目编码-获取申请单", description = "根据项目编码-获取申请单", method = "GET")
    public Result<List<ProjectPauseOperation>> getList(@PathVariable(name = "proCode", required = true) String proCode) {
        List<ProjectPauseOperation> list = projectPauseOperationService.list(ProjectPauseOperation.class, Criteria.create().addCriterion("proCode", Operator.EQ, proCode));
        return Result.OK(list);
    }

    @GetMapping("one/{bizNo}")
    @Operation(summary = "根据暂停申请单-获取正在审批的申请单", description = "根据暂停申请单-获取正在审批的申请单", method = "GET")
    public Result<ProjectPauseOperation> getOne(@PathVariable(name = "bizNo", required = true) String bizNo) {
        ProjectPauseOperation pauseOperation = projectPauseOperationService.getOne(bizNo);
        return Result.OK(pauseOperation);
    }

    @GetMapping("detail/{bizNo}")
    @Operation(summary = "根据申请单编码-获取正在审批的申请单详情内容", description = "根据申请单编码-获取正在审批的申请单详情内容", method = "GET")
    public Result<?> getDetail(@PathVariable(name = "bizNo", required = true) String bizNo) {
        return projectPauseOperationService.getDetail(bizNo);
    }
}
